<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>JavaScripTools Manual</title>
    <link rel="stylesheet" type="text/css" href="../../css/style.css">
</head>

<body>
<br>
<h1>JavaScripTools Manual</h1>
<h2>JavaScriptUtil</h2>
<h3>Working with strings</h3>
<p>There are several functions to perform operations on strings.</p>
<p>The best place to look at the 
<a href="../api/script_JavaScriptUtil.html">JavaScriptUtil API documentation</a>,
that have a complete list of functions. Here, the idea is not to replicate that
information, but to give a basic understanding.</p>
<br>
<p>The
<a href="../api/JavaScriptUtil_functions.html#trim">trim</a>,
<a href="../api/JavaScriptUtil_functions.html#ltrim">ltrim</a>,
<a href="../api/JavaScriptUtil_functions.html#rtrim">rtrim</a>,
<a href="../api/JavaScriptUtil_functions.html#crop">crop</a>,
<a href="../api/JavaScriptUtil_functions.html#lcrop">lcrop</a> and
<a href="../api/JavaScriptUtil_functions.html#rcrop">rcrop</a>
functions remove unwanted characters from the string. trim, ltrim and rtring removes 
whitespaces, while crop removes a number of characters from the string.</p>
Examples:
<pre>trim(" HELLO ") -> "HELLO"
ltrim("\nHELLO\n") -> "HELLO\n"
rtrim(" HELLO ") -> " HELLO"
crop("ABCDEFGH", 4, 2) -> "ABCDGH"
lcrop("ABCDEFGH", 3) -> "DEFGH"
rcrop("ABCDEFGH", 3) -> "ABCDE"
</pre><br>
<p>The
<a href="../api/JavaScriptUtil_functions.html#lpad">lpad</a> and
<a href="../api/JavaScriptUtil_functions.html#rpad">rpad</a>
functions returns a string with, at least, the specified length, completing 
it on the left or right with a specified character (a space by default).</p>
Examples:
<pre>lpad("a", 5) -> "    a"
lpad("10", "6", "0") -> "000010"
lpad("ABCDE", "3", "/") -> "ABCDE" //The size is not reduced
rpad("A", 3, "*") -> "A**"
rpad("", 5, "-") -> "-----"
rpad("12345", "2", " ") -> "12345" //The size is not reduced
</pre><br>
<p>The
<a href="../api/JavaScriptUtil_functions.html#replaceAll">replaceAll</a>,
<a href="../api/JavaScriptUtil_functions.html#insertString">insertString</a> and
<a href="../api/JavaScriptUtil_functions.html#capitalize">capitalize</a>
functions calculate "changes" the string (strings are immutable - cannot be changed), 
returning the new string.</p>
Examples:
<pre>replaceAll("I am NAME", "NAME", "John") -> "I am John"
insertString("IJohn", 1, " am ") -> "I am John"
capitalize("i am john") -> "I Am John"
</pre><br>
<p>The
<a href="../api/JavaScriptUtil_functions.html#onlySpecified">onlySpecified</a>,
<a href="../api/JavaScriptUtil_functions.html#onlyLetters">onlyLetters</a>,
<a href="../api/JavaScriptUtil_functions.html#onlyNumbers">onlyNumbers</a> and
<a href="../api/JavaScriptUtil_functions.html#onlyAlpha">onlyAlpha</a> 
functions test if a given string contains only certain characters.</p>
Examples:
<pre>onlySpecified("123213321", "123") -> true
onlySpecified("123213321", "0123") -> false
onlyLetters("Testing") -> true
onlyLetters("Just testing") -> false (there's a space)
onlyAlpha("abc123") -> true
</pre><br>
<p>The
<a href="../api/JavaScriptUtil_functions.html#left">left</a>,
<a href="../api/JavaScriptUtil_functions.html#right">right</a> and
<a href="../api/JavaScriptUtil_functions.html#mid">mid</a>
functions select a part of a given string, returning the substring. Those functions
use the standard substring method, and are just other ways of using that method,
because they care about number of characters, not indexes (except the mid second parameter,
because it is an index).</p>
Examples:
<pre>left("Home sweet home", 4) -> "Home"
right("Home sweet home", 4) -> "home"
mid("Home sweet home", 5, 3) -> "swe"
</pre><br>
<p>The
<a href="../api/JavaScriptUtil_functions.html#escapeCharacters">escapeCharacters</a> and
<a href="../api/JavaScriptUtil_functions.html#unescapeCharacters">unescapeCharacters</a>
functions escape / unescape a string, so it can be written inside a dynamic JavaScript block
inside a string, without messing the code.</p>
Examples:
<pre>var str = "line1\nline2";
eval("alert('" + str + "')") -> ERROR!!!
eval("alert('" + escapeCharacters(str) + "')") -> OK
unescapeCharacters("abc\\ndef") -> "abc\ndef"
</pre><br>

<br><br>
<hr>
<table width="100%">
    <tr>
        <td width="33%" align="left"><b>Previous:</b><br><a href="JavaScriptUtil_constants.html">The declared constants</a></td>
        <td width="34%" align="center"><a href="index.html">Table of Contents</a></td>
        <td width="33%" align="right"><b>Next:</b><br><a href="JavaScriptUtil_objects.html">Working with objects</a></td>
    </tr>
</table>

</body>
</html>
